﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Commonlib;
namespace AlbonWeb.Manager.MenuManager
{
    public partial class MenuAdd : System.Web.UI.Page
    {
        private readonly BLL.MenuBLL menubll = new BLL.MenuBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定父菜单
                string cmdst = "select * from Menu where parentID=0";
                SqlHelper.BindDropDownList(ddlTop, "menuName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdst, null);
                ddlTop.Items.Insert(0,new ListItem("--no top--","0"));
                if (Request["id"] != null)
                {
                    initData(Request["id"].ToString());
                }
                if (Request["menuname"] != null)
                {
                    menuName.Text = Request["menuname"].ToString();
                }
                if (Request["pid"] != null)
                {
                    ddlTop.SelectedValue = Request["pid"].ToString();
                }
                bindDDL(ddlTop.SelectedValue);
            }
        }

        protected void initData(string id)
        {
            
            DataTable dt=menubll.getMenuByID(id);
            if (dt.Rows.Count != 0)
            {
                menuName.Text = dt.Rows[0]["menuName"].ToString();
                ddlTop.SelectedValue = dt.Rows[0]["parentID"].ToString();
                url.Text = dt.Rows[0]["url"].ToString();
                remark.Text = dt.Rows[0]["remark"].ToString();
                string pid=dt.Rows[0]["parentID"].ToString();
            }
            btnSubmit.Text = "Update";
        }
        //获取所有同级菜单
        protected void bindDDL(string pid)
        { 
              ddlMenu.DataSource=menubll.getMenuByPid(pid);
                ddlMenu.DataTextField = "menuName";
                ddlMenu.DataValueField = "sequence";
                ddlMenu.DataBind();
                ddlMenu.Items.Insert(0,new ListItem("--don't change--","0"));
        }
        //添加或更新
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Albon.Model.Menu menu = new Albon.Model.Menu();
            if (Request["id"] != null)
            {
                menu.ID = int.Parse(Request["id"].ToString());
            }
            else
            {
                menu.ID = 0;
            }
            menu.menuName = menuName.Text.Trim();
            menu.parentID = int.Parse(ddlTop.SelectedValue);
            menu.url = url.Text.Trim();
            menu.remark = remark.Text.Trim();
            menu.Sequence = int.Parse(ddlMenu.SelectedValue);
            BLL.MenuBLL menubll = new BLL.MenuBLL();
            menubll.alterMenu(menu);
            Response.Redirect("/Manager/MenuManager/MenuList.aspx");
        }
        protected void ddlTop_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindDDL(ddlTop.SelectedValue);
        }
    }
}